package feng.demo.service;

import feng.demo.dao.CartDao;
import feng.demo.dao.CommodityDao;
import feng.demo.dao.UserDao;
import feng.demo.entity.Cart;
import feng.demo.entity.Commodity;
import feng.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@CacheConfig(cacheNames = {"cart"})
public class CartServiceImpl implements CartService{
    @Autowired
    CartDao cartDao;
    @Autowired
    UserDao userDao;
    @Autowired
    CommodityService commodityService;
    @Override
    @Cacheable
    public List<Cart> findAll() {
        List<Cart> list = cartDao.findAll();
        return adds(list);
    }

    @Override
    @Cacheable
    public List<Cart> findAllLimit(Integer pagenum, Integer lines) {
        List<Cart> list = cartDao.findAllLimit(pagenum, lines);
        return adds(list);
    }

    @Override
    @Cacheable
    public List<Cart> findCart(Map<String, Object> map) {
        List<Cart> l = cartDao.findCart(map);
        return adds(l);
    }

    @Override
    @CacheEvict(value = "cart", allEntries = true)
    public int add(Cart cart) {
        return cartDao.add(cart);
    }

    @Override
    @CacheEvict(value = "cart", allEntries = true)
    public int del(Integer cartid) {
        return cartDao.del(cartid);
    }

    @Override
    @CacheEvict(value = "cart", allEntries = true)
    public int delUidComid(Integer uid, Integer comid) {
        return cartDao.delUidComid(uid,comid);
    }

    @Override
    @CacheEvict(value = "cart", allEntries = true)
    public int update(Cart cart) {
        return cartDao.update(cart);
    }

    @Override
    @Cacheable(key = "#p0")
    public Cart findById(Integer cartid) {
        Cart cart = cartDao.findById(cartid);
        return doadd(cart);
    }

    @Override
    @Cacheable(key = "#p0")
    public List<Cart> findByUid(Integer uid) {
        List<Cart> list = cartDao.findByUid(uid);
        return adds(list);
    }

    @Override
    @Cacheable(key = "#p0")
    public Cart findByComid(Integer comid) {
        Cart cart = cartDao.findByComid(comid);
        return doadd(cart);
    }

    @Override
    public int findCount() {
        return cartDao.findCount();
    }

    private List<Cart> adds(List<Cart> list){
        for (Cart c:list) {
            Commodity com = commodityService.findById(c.getCommodity().getComid());
            User user = userDao.findByUid(c.getUser().getUid());
            c.setCommodity(com);
            c.setUser(user);
        }

        return list;
    }
    private Cart doadd(Cart c){
        Commodity com = commodityService.findById(c.getCommodity().getComid());
        User user = userDao.findByUid(c.getUser().getUid());
        c.setCommodity(com);
        c.setUser(user);
        return c;
    }
}
